﻿using System.Windows.Forms;
using Nuwanda.Common;
using Nuwanda.TfsSpotlight.Data;
using Nuwanda.TfsSpotlight.Shell.Proxies.Interfaces;
using Nuwanda.TfsSpotlight.Shell.Services.Interfaces;

namespace Nuwanda.TfsSpotlight.Views
{
    /// <summary>Serves as a base class for tabs presenters</summary>
    /// <typeparam name="T">Type of a view</typeparam>
    [Author("Bartłomiej Legiędź")]
    public abstract class TabPresenterBase<T> : ITabPresenter
    {
        protected T View { get; set; }
        protected IWorker Worker { get; set; }
        protected IMenuService MenuService { get; set; }
        protected ILogger Logger { get; set; }

        /// <summary>A control to place on a tab</summary>
        public Control Control
        {
            get { return this.View as Control; }
        }

        /// <summary>A tab associated with this presenter</summary>
        public ITabStripItem Tab { get; set; }

        /// <summary>Indicates tab's active state</summary>
        public bool IsActive { get; protected set; }

        protected TabPresenterBase(T view, IWorker worker, IMenuService menuService, ILogger logger)
        {
            this.IsActive = false;
            this.View = view;
            this.Worker = worker;
            this.MenuService = menuService;
            this.Logger = logger;
        }

        /// <summary>Indicates whetcher the tab is working</summary>
        public bool IsWorking
        {
             get { return this.Worker.IsWorking; }
        }

        public virtual void HandleActivation()
        {
            this.IsActive = true;
            this.InvalidateHandledCommands();
        }

        public virtual void HandleDeactivation()
        {
            this.IsActive = false;
            this.DisableHandledCommands();
        }

        public virtual void HandleClose() {}

        public virtual void HandleRefresh() {}

        public virtual void HandleSave() {}

        public abstract void HandleLoad();

        public abstract bool IsEqualTo(ITabPresenter presenter);

        public abstract string GetCaption();

        public virtual bool IsDirty { get { return false; } }


        public virtual bool BelongsToServer(ServerDescriptor serverDescriptor)
        {
            return false;
        }

        public virtual void WaitTillTheEndOfTheWork()
        {
            this.Worker.WaitTillEnd();
        }

        public virtual void InvalidateHandledCommands()
        {
            if(this.IsActive)
                this.MenuService.InvalidateCommands(this);
        }

        public virtual void DisableHandledCommands()
        {
            this.MenuService.DisableCommands(this);
        }

        protected bool DefaultCommandActivator()
        {
            return this.IsActive && this.IsWorking == false && this.Tab.Locked == false;
        }

        public void HandleLock()
        {
            this.Tab.Locked = true;

            if (this.IsActive)
            {
                this.InvalidateHandledCommands();
            }
        }

        public void HandleUnLock()
        {
            this.Tab.Locked = false;

            if(this.IsActive)
            {
                this.InvalidateHandledCommands();
            }
        }
    }
}
